﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows;
using System.Windows.Media;

namespace wpf.Nursing.Common
{
    public class NursingCancas : Canvas
    {
    }



    /// <summary>
    /// 为Grid添加的一个特殊功能
    /// </summary>
    public class GridHelper
    {
        //边框的宽度 定义默认值
        static double myBorderWidth = 2;//
        static string BorderBrushcolor = "#eee";
        public static double GetBorderWidth(System.Windows.DependencyObject obj)
        {
            return (double)obj.GetValue(BorderWidthProperty);
        }

        public static void SetBorderWidth(System.Windows.DependencyObject obj, double value)
        {
            obj.SetValue(BorderWidthProperty, value);
        }
        public static string GetBorderBrush(System.Windows.DependencyObject obj)
        {
            return (string)obj.GetValue(borderBrushs);
        }
        public static void SetBorderBrush(System.Windows.DependencyObject obj, string value)
        {
            obj.SetValue(borderBrushs, value);
        }
        //自定义边框宽度依赖属性
        public static readonly DependencyProperty BorderWidthProperty =
                DependencyProperty.RegisterAttached("BorderWidth", typeof(double), typeof(GridHelper), new PropertyMetadata(OnBorderWidthChanged));
        //自定义边框颜色依赖属性 
        public static readonly DependencyProperty borderBrushs =
            DependencyProperty.RegisterAttached("BorderBrush", typeof(String), typeof(GridHelper), new PropertyMetadata(borderBrush));
        private static void OnBorderWidthChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            myBorderWidth = (double)e.NewValue;//更改后属性的值
        }
        private static void borderBrush(DependencyObject b, DependencyPropertyChangedEventArgs e)
        {
            BorderBrushcolor = (string)e.NewValue;//
        }
        public static bool GetShowBorder(DependencyObject obj)
        {
            return (bool)obj.GetValue(ShowBorderProperty);
        }
        public static void SetShowBorder(DependencyObject obj, bool value)
        {
            obj.SetValue(ShowBorderProperty, value);
        }
        /// <summary>
        /// 使用public static声明一个DependencyProperty变量
        /// </summary>
        public static readonly DependencyProperty ShowBorderProperty =
                DependencyProperty.RegisterAttached("ShowBorder", typeof(bool), typeof(GridHelper), new PropertyMetadata(ShowBorder));




        //静态方法
        private static void ShowBorder(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var grid = d as Grid;
            if ((bool)e.OldValue)
            {
                grid.Loaded -= (s, arg) => { };
            }
            if ((bool)e.NewValue)
            {
                grid.Loaded += (s, arg) =>
                {
                    //根据Grid的顶层子控件的个数去添加边框，同时考虑合并的情况
                    var controls = grid.Children;
                    var count = controls.Count;
                    for (int i = 0; i < count; i++)
                    {
                        var item = controls[i] as FrameworkElement;
                        var row = Grid.GetRow(item);
                        var column = Grid.GetColumn(item);
                        var rowspan = Grid.GetRowSpan(item);
                        var columnspan = Grid.GetColumnSpan(item);
                        //设置边框线的颜色  #FFD3F1FE
                        var border = new Border();
                        Color color = (Color)ColorConverter.ConvertFromString(BorderBrushcolor);
                        border.BorderBrush = new SolidColorBrush(color);
                       // border.BorderBrush = new SolidColorBrush(Colors.Black);
                        if (row == grid.RowDefinitions.Count - 1 && column == grid.ColumnDefinitions.Count - 1)
                            border.BorderThickness = new Thickness(myBorderWidth);
                        else if (row == grid.RowDefinitions.Count - 1)
                            border.BorderThickness = new Thickness(myBorderWidth, myBorderWidth, 0, myBorderWidth);

                        else if (rowspan > 1 && row + rowspan == grid.RowDefinitions.Count)
                            border.BorderThickness = new Thickness(myBorderWidth, myBorderWidth, myBorderWidth, myBorderWidth);
                        else if (columnspan > 1 && column + columnspan == grid.ColumnDefinitions.Count)
                            border.BorderThickness = new Thickness(myBorderWidth, myBorderWidth, myBorderWidth, myBorderWidth);
                        else if (column == grid.ColumnDefinitions.Count - 1)
                            border.BorderThickness = new Thickness(myBorderWidth, myBorderWidth, myBorderWidth, 0);
                        else
                            border.BorderThickness = new Thickness(myBorderWidth, myBorderWidth, 0, 0);
                        //double left= myBorderWidth;
                        //double top = myBorderWidth;
                        //double right =0;
                        //double bottom =0;
                        //if (column == grid.ColumnDefinitions.Count - 1 || column + columnspan == grid.ColumnDefinitions.Count)
                        //{
                        //    right = myBorderWidth;
                        //}
                        //if (row == grid.RowDefinitions.Count - 1 || row + rowspan == grid.RowDefinitions.Count)
                        //{
                        //    bottom = myBorderWidth;
                        //}
                        //border.BorderThickness = new Thickness(left, top, right, bottom);
                        Grid.SetRow(border, row);
                        Grid.SetColumn(border, column);
                        Grid.SetRowSpan(border, rowspan);
                        Grid.SetColumnSpan(border, columnspan);
                        grid.Children.Add(border);
                    }
                };
            }
        }
    }


}
